
//--> Loth 2011 
//--> Alternativa engine & interface init
//--> link: http://alternativaplatform.com/

package chamans.engine 
{
	import alternativa.engine3d.core.*;
	import alternativa.engine3d.lights.DirectionalLight;
	import alternativa.engine3d.lights.OmniLight;
	import alternativa.engine3d.materials.*;
	import alternativa.engine3d.primitives.*;
	import alternativa.engine3d.resources.*;
	import alternativa.engine3d.lights.*;
	
	import chamans.*;
	
	import flash.geom.*;
	import flash.system.*;
	import flash.events.*;
	import flash.display.*;
	import flash.ui.Keyboard;
	
	public class Alternativa extends Sprite
	{
		public static var stage:Stage;
		public var FlashLimiteVersion:Array=[10, 0, 3, 181];
		public var physicEngine:String;
		public var Engine:String;
		public var act:Boolean=false;
		// chamans 
		public var KEY:key;
		public var LOAD:load;
		public var TOOL:Tool;
		public var SYS:Interface;
		public var PAUSE:PauseScreen;
		// init variable
		private var END:Function;
		private var LOOP:Function;
		private var def:Object;
		// basic variable ?
		public var autoMove:Boolean=false;
		public var mouseHide:Boolean=false;
		public var MenuActif:Boolean=false;
		// resize variable
		public var limite:Boolean=true;
		public var size:Point=new Point(0,0);
		public var sizelimit:Point=new Point(800,450);
		
		//______________________o
		// Alternativa 8.5 variable
		public var view:View;
		public var stage3D:Stage3D;
		private var scene:Object3D;
		private var Camera:Camera3D;
		// variable
		private var box:Box;
		public var LIGHT:Array;
		public var physics:*;
		public var FogColor:int;
		public var FogDistance:int;

		//_______________________________________________loop function
		private function OO(event:Event=null):void
		{
			//if (Camera) Camera.update();
			if(LOOP!=null)LOOP();
			if(box)box.rotationZ -= 0.01;
			Camera.render(stage3D);
		}
		public function Alternativa(end:Function=null, loop:Function=null, setting:Object=null)
		{
			Engine="alternativa";
			END=end;
			LOOP=loop;
			def = setting || new Object();
			if(stage) init(); // -> flash builder
			else addEventListener(Event.ADDED_TO_STAGE,onStage);// -> flash cs5
		}
		private function onStage(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,onStage);
			init();
		}
		private function init():void
		{
			// chamans base
			LOAD = new load(); TOOL = new Tool(); KEY = new key(stage);
			// stage set
			with (stage){ align = "TL"; scaleMode = "noScale"; quality = "HIGH"; }
			//stage.addEventListener('resize',  RESIZE, false, -1, true);
			init_2D();
		}
		
		//_______________________________________________2D interface init
		private function init_2D():void
		{	
			SYS = new Interface(this, init_VIEW3D, def);
			addChild(SYS);
		}
		//_______________________________________________3d Engine init
		private function init_VIEW3D():void
		{
			Camera = new Camera3D(0.1, 1500);
			Camera.view = new View(stage.stageWidth, stage.stageHeight);
			addChildAt(Camera.view,0);
			//addChild(camera.diagram);
			view=Camera.view;
			// Initial position
			Camera.rotationX = -90*Math.PI/180;
			Camera.y = -1000;
			Camera.z = 0;
			scene = new Object3D();
			scene.addChild(Camera);
			
			var Light:OmniLight=new OmniLight(0x0000FF, 500, 1000)
			Light.y=300
			Light.z = 500;
			scene.addChild(Light);
			
			FogDistance = def.FogDistance || 1000
			FogColor = def.FogColor || 0x413A2E
			var Light2:AmbientLight=new AmbientLight(0xFF0000)
			//Light2.lookAt(1,0,0)
			scene.addChild(Light2);
			//O.LOAD.IMG( "assets/crate.jpg", initTexture)
			LOAD.IMGLIST("assets/demobullet/",["panel_d2", "panel_local", "panel_s"],initTexture, ".jpg" )
			//initTexture()
		}
		private function initTexture(A:Array=null):void{
			// Primitive box
			box = new Box(500, 500, 500, 5, 5, 5);
			var material:FillMaterial = new FillMaterial(0xFF7700);
			var texture:BitmapTextureResource = new BitmapTextureResource(A[0]);
			var texture_norm:BitmapTextureResource = new BitmapTextureResource(A[1]);
			var texture_luma:BitmapTextureResource = new BitmapTextureResource(A[2]);
			var material2:TextureMaterial = new TextureMaterial(texture);
			var material3:LightMapMaterial = new LightMapMaterial(texture, texture_luma)
			var material4:StandardMaterial = new StandardMaterial(texture, texture_norm, texture_luma, null )
			//box.setMaterialToAllSurfaces(material);
			box.setMaterialToAllSurfaces(material3);
			scene.addChild(box);
			
			stage3D = stage.stage3Ds[0];
			stage3D.addEventListener(Event.CONTEXT3D_CREATE, onContextCreate);
			stage3D.requestContext3D();
		}
		private function onContextCreate(e:Event):void {
			stage3D.removeEventListener(Event.CONTEXT3D_CREATE, onContextCreate);
			for each (var resource:Resource in scene.getResources(true)) {
				resource.upload(stage3D.context3D);
			}
			RESIZE()
			Active()
			if(END!=null)END()
		}
		//_______________________________________________rezise
		public function RESIZE(e:Event=null):void
		{
			size=new Point( stage.stageWidth, stage.stageHeight);
			SYS.OUT("resize "+size.x+" "+size.y);
			if(SYS.actif) SYS.cadre(size);
			if(PAUSE) PAUSE.Resize();
			if(view) RESIZE3D();
			if(limite && size.x==sizelimit.x && size.y==sizelimit.y)activeResize(false);
		}
		public function RESIZE3D(e:Event=null):void
		{
			view.width = size.x; 
			view.height=size.y;
		}
		public function activeResize(n:Boolean=true):void 
		{
			if(n)stage.addEventListener('resize',  RESIZE, false, 0, true);
			else {stage.removeEventListener('resize',  RESIZE); 
			SYS.OUT(SYS.MESSAGE);
		}}
		//_______________________________________________Pause screen
		private function Active(e:Event=null):void
		{
			stage.removeEventListener(Event.ACTIVATE,Active);
			stage.addEventListener(Event.ENTER_FRAME, OO, false, 0, true);
			stage.addEventListener(Event.DEACTIVATE,Desactive, false, 0, true);
			if(PAUSE!=null){PAUSE.clear(); removeChild(PAUSE);PAUSE=null}
		}
		private function Desactive(e:Event):void 
		{
			stage.removeEventListener(Event.ENTER_FRAME, OO);
			stage.removeEventListener(Event.DEACTIVATE,Desactive);
			stage.addEventListener(Event.ACTIVATE,Active, false, 0, true);
			if(PAUSE==null) PAUSE=new PauseScreen(this); addChild(PAUSE);
		}
}}